package com.kx.service_exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kx.service_exam.common.vo.BaseVO;
import com.kx.service_exam.dao.QuestionInfoMapper;
import com.kx.service_exam.dao.QuestionInfoMapper_My;
import com.kx.service_exam.exception.BusinessException;
import com.kx.service_exam.model.*;
import com.kx.service_exam.service.interfaces.IExamModuleSV;
import com.kx.service_exam.service.interfaces.IQuestionChoiceSV;
import com.kx.service_exam.service.interfaces.IQuestionInfoSV;
import com.kx.service_exam.vo.query.QuestionInfoQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class QuestionInfoSVImpl implements IQuestionInfoSV {

    @Autowired
    private QuestionInfoMapper questionInfoMapper;

    @Autowired
    private QuestionInfoMapper_My questionInfoMapper_my;

    @Autowired
    private IQuestionChoiceSV questionChoiceSV;

    @Autowired
    private IExamModuleSV examModuleSV;

    @Override
    public int insertQuestionInfo(QuestionInfo questionInfo) {
        Date date = new Date();
        questionInfo.setId(UUID.randomUUID().toString());
        questionInfo.setCreate_time(date);
        questionInfo.setModify_time(date);
        return questionInfoMapper.insertSelective(questionInfo);
    }

    @Override
    public boolean addQuestionInfoDetail(QuestionInfoDetail questionInfoDetail, String operatorId) {
        if(questionInfoDetail==null) {
            throw new BusinessException("[questionInfoDetail]不能为空");
        }
        if (StringUtils.isEmpty(questionInfoDetail.getExam_module_id())){
            throw new BusinessException("[(questionInfoDetail.getExam_module_id]不能为空");
        }
        ExamModule examModule = examModuleSV.selectExamModuleById(questionInfoDetail.getExam_module_id());
        if(examModule == null) {
            throw new BusinessException("没有关联的examModule");
        }
        QuestionInfo questionInfo = new QuestionInfo();
        BeanUtils.copyProperties(questionInfoDetail,questionInfo);
        questionInfo.setDelete_flag(0);
        questionInfo.setCreator(operatorId);
        questionInfo.setModifier(operatorId);
        questionInfo.setWrong_num(0);
        questionInfo.setRight_num(0);
        questionInfo.setQuestion_context(questionInfo.getQuestion_info());
        insertQuestionInfo(questionInfo);
        System.out.println("questionInfo.id" + questionInfo.getId());
        if(!questionInfoDetail.getQuestionChoices().isEmpty()){
            questionChoiceSV.batchInsertQuestionChoice(questionInfoDetail.getQuestionChoices(),questionInfo.getId(),operatorId);
        }
        return true;
    }

    @Override
    public boolean logicalDeleteQuestionInfoDetailById(String id, String operatorId) {
        if(StringUtils.isEmpty(id)) {
            throw new BusinessException("[id]不能为空");
        }
        QuestionInfo questionInfo = new QuestionInfo();
        questionInfo.setDelete_flag(1);
        questionInfo.setModifier(operatorId);
        questionInfo.setId(id);
        int result = questionInfoMapper.updateByPrimaryKeySelective(questionInfo);
        if (result == 0) {
            throw new BusinessException("没有可以删除的记录");
        }
        questionChoiceSV.logicalDeleteQuestionChoiceByQuestionId(id);
        return true;
    }

    @Override
    public int deleteQuestionInfo(String id) {
        return questionInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean deleteQuestionInfoDetailById(String id) {
        if(StringUtils.isEmpty(id)) {
            throw new BusinessException("[id]不能为空");
        }

        int result = deleteQuestionInfo(id);
        if (result == 0) {
            throw new BusinessException("没有可以删除的记录");
        }
        questionChoiceSV.deleteQuestionChoiceByQuestionId(id);
        return true;
    }

    @Override
    public int updateQuestionInfo(QuestionInfo questionInfo) {
        if(StringUtils.isEmpty(questionInfo.getId())) {
            throw new BusinessException("[questionInfo.id]不能为空");
        }
        questionInfo.setModify_time(new Date());
        return questionInfoMapper.updateByPrimaryKey(questionInfo);
    }

    @Override
    public QuestionInfo selectQuestionInfoById(String id) {
        if(StringUtils.isEmpty(id)) {
            throw new BusinessException("[questionInfo.id]不能为空");
        }
        return questionInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public long countQuestionInfoByFilter(QuestionInfoQueryVO filter) throws ParseException {
        return questionInfoMapper.countByExample(this.convertFilterToExample(filter));
    }

    @Override
    public long countQuestionInfoByExamModuleId(String examModuleId) throws ParseException {
        if(StringUtils.isEmpty(examModuleId)) {
            throw new BusinessException("参数[examModuleId]不能为空!");
        }
        QuestionInfoQueryVO filter = new QuestionInfoQueryVO();
        filter.setDelete_flag(0);
        filter.setExam_module_id(examModuleId);
        return countQuestionInfoByFilter(filter);
    }

    @Override
    public List<QuestionInfo> lisQuestionInfoByFilter(QuestionInfoQueryVO filter) throws ParseException {
        return questionInfoMapper.selectByExample(this.convertFilterToExample(filter));
    }

    @Override
    public List<QuestionInfo> listQuestionInfoByExamModuleId(String examModuleId) throws ParseException {
        if(StringUtils.isEmpty(examModuleId)) {
            throw new BusinessException("参数[examModuleId]不能为空!");
        }
        QuestionInfoQueryVO filter = new QuestionInfoQueryVO();
        filter.setDelete_flag(0);
        filter.setExam_module_id(examModuleId);
        return lisQuestionInfoByFilter(filter);
    }

    @Override
    public PageInfo<QuestionInfo> pageQuestionInfoByFilter(QuestionInfoQueryVO filter) throws ParseException {
        if(filter !=null) {
            if(filter.getPage_index() != null && filter.getPage_size() != null){
                PageHelper.startPage(filter.getPage_index(), filter.getPage_size());
            }
        }

        List<QuestionInfo> list = lisQuestionInfoByFilter(filter);
        return new PageInfo<QuestionInfo>(list);
    }

    @Override
    public List<QuestionInfoDetail> listQuestionInfoDetail(QuestionInfoQueryVO filter) throws ParseException {

        return questionInfoMapper_my.listQuestionInfoDetail(filter);
    }

    @Override
    public QuestionInfoDetail selectQuestionInfoDetailById(String id) throws ParseException {
        if(StringUtils.isEmpty(id)) {
            throw new BusinessException("参数[id]不能为空!");
        }
        QuestionInfoQueryVO filter = new QuestionInfoQueryVO();
        filter.setId(id);
        List<QuestionInfoDetail> list =  listQuestionInfoDetail(filter);
        if(list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public QuestionInfoExample convertFilterToExample(QuestionInfoQueryVO filter) {
        if(filter == null) {
            return null;
        }
        QuestionInfoExample questionInfoExample = new QuestionInfoExample();
        QuestionInfoExample.Criteria criteria = questionInfoExample.or();
        if(!StringUtils.isEmpty(filter.getDelete_flag())){
            criteria.andDelete_flagEqualTo(filter.getDelete_flag());
        }
        if(!StringUtils.isEmpty(filter.getDifficulty_level())) {
            criteria.andDifficulty_levelEqualTo(filter.getDifficulty_level());
        }
        if(!StringUtils.isEmpty(filter.getExam_module_id())) {
            criteria.andExam_module_idEqualTo(filter.getExam_module_id());
        }
        if(!StringUtils.isEmpty(filter.getQuestion_context())) {
            criteria.andQuestion_contextLike(filter.getQuestion_context());
        }
        if(!StringUtils.isEmpty(filter.getQuestion_info())) {
            criteria.andQuestion_contextLike(filter.getQuestion_info());
        }
        if(!StringUtils.isEmpty(filter.getQuestion_type())) {
            criteria.andQuestion_typeEqualTo(filter.getQuestion_type());
        }
        return questionInfoExample;
    }
}
